package com.xayy.core.antlr4.utils;

import cn.kawins.base.utils.BeanUtil;
import com.xayy.core.antlr4.enums.ValueTypeEnum;
import com.xayy.core.antlr4.exception.CheckSyntaxException;
import com.xayy.core.antlr4.model.ErrorResult;
import com.xayy.core.antlr4.visitor.HealthEvaluateVisitor;
import com.xayy.mgt.model.AssociationParameter;
import com.xayy.mgt.service.AssociationParameterService;
import com.xayy.mgt.service.impl.OutInterfaceServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/8/16 15:08
 */
@Component
@Slf4j
public class CsUtil {

    private static CsUtil csUtil;

    @Autowired
    private AssociationParameterService accService;

    @PostConstruct
    public void init() {
        csUtil = this;
        csUtil.accService = this.accService;
    }

    public static Double getCSScore(Long satelliteId, Long nodeId, Date evaluationTime, String name,
                                    Boolean checkSyntax, Boolean errorEvaluation) {
        String code = name.substring(3);
        // 先从缓存中获取有没有
        Double vaule;
        if (Boolean.TRUE.equals(errorEvaluation)) {
            vaule = Golabl.getErrorVaule(satelliteId, nodeId, code, ValueTypeEnum.GC_VALUES.getKey());
        } else {
            vaule = Golabl.getVaule(satelliteId, nodeId, code, ValueTypeEnum.GC_VALUES.getKey());
        }
        if (vaule != null)
            return vaule;
        // 调用脚本计算
        // 查询该条性能参数信息获取执行语法
        AssociationParameter associationParameter =
                csUtil.accService.selectOne(AssociationParameter.builder().nodeId(nodeId).metricsCode(code).build());
        if (BeanUtil.isEmpty(associationParameter)) {
            log.error("关联参数:" + name + ",不存在");
            throw new CheckSyntaxException("关联参数:" + name + ",不存在");
        }
        String customParam = associationParameter.getCustomParam();
        HealthEvaluateVisitor visitor = new HealthEvaluateVisitor(satelliteId, associationParameter.getParamNodeId(),
                evaluationTime, checkSyntax, errorEvaluation);
        ErrorResult execute = FunUtil.execute(customParam, visitor);
        if (execute.isSuccess()) {
            Double result = execute.getResult();
            if (Boolean.FALSE.equals(checkSyntax)) {
                if (Boolean.TRUE.equals(errorEvaluation)) {
                    Golabl.setErrorValue(satelliteId, nodeId, code, result, ValueTypeEnum.GC_VALUES.getKey());
                } else {
                    Golabl.setValue(satelliteId, nodeId, code, result, ValueTypeEnum.GC_VALUES.getKey());
                }

            }
            OutInterfaceServiceImpl.addLogs("关联参数："+associationParameter);
            OutInterfaceServiceImpl.addLogs("关联参数结果："+execute.getResult());
            return execute.getResult();
        }
        return 0D;
    }
}
